//
//  MYMainController.swift
//  MYSinaWeico
//
//  Created by Magic Yu on 16/4/6.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit

class MYMainController: UITabBarController {

    //按钮的点击事件不希望外界访问 可以加上private关键字限制其调用范围
    //[MYSinaWeico.MYMainController plusBtnDidClick]: unrecognized selector sent to instance 0x7fe72be890d0
    //swift是一个静态语言
    //加上private后 对于本类可见 对于运行循环的消息机制不可见 所以找不到方法
    @objc private func plusBtnDidClick() {
    
        print("plusBtn Click")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //自定义tabBar
        let mainTabBar = MYMainTabBar()
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        print(tabBar.classForCoder)
        setValue(mainTabBar, forKey: "tabBar")
        print(tabBar.classForCoder)
        
        
        //添加子控制器
        addChildViewControllers()
        
    }
    
    //MARK:添加子控制器
    private func addChildViewControllers() {
    
        addChildViewController(MYHomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MYMessageController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(MYDiscoverController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(MYProfileController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    
    //privite 只有在当前文件才能访问该方法
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        
        //这里不能创建新的控制器,应该用传进来的控制器
//        let vc = UIViewController()
        vc.title = title
//        vc.navigationItem.title = title
//        vc.tabBarItem.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        let nav = MYBaseViewController(rootViewController: vc)
        
        addChildViewController(nav)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}



